package com.baizhi.dao;

import com.baizhi.domain.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

// 自定义接口需要继承JpaRepository<实体类类型, 实体类主键类型>, JpaSpecificationExecutor<实体类类型>
public interface Article3Dao extends JpaRepository<Article, Integer>, JpaSpecificationExecutor<Article> {

    // 展示位置参数绑定 [按照title和author查询]
    // 占位符从1开始
    @Query("from Article a where a.title = ?1 and a.author = ?2")
    List<Article> findByCondition1(String title, String author);

    // 展示名字参数绑定
    @Query("from Article a where a.title = :title and a.author = :author")
    List<Article> findByCondition2(@Param("title") String title, @Param("author") String author);

    // 展示like模糊绑定
    @Query("from Article a where a.title like %:title%")
    List<Article> findByCondition3(@Param("title") String title);

    // 展示排序查询
    @Query("from Article a where a.title like %:title% order by a.aid desc")
    List<Article> findByCondition4(@Param("title") String title);

    // 展示分页查询
    @Query("from Article a where a.title like %:title%")
    Page<Article> findByCondition5(Pageable pageable, @Param("title") String title);

    // 展示传入集合参数查询
    @Query("from Article a where a.aid in :aids")
    List<Article> findByCondition6(@Param("aids") List<Integer> aids);

    // 展示传入Bean进行查询(SPEL表达式查询)
    @Query("from Article a where a.title = :#{#article.title} and a.author = :#{#article.author}")
    List<Article> findByCondition7(@Param("article") Article article);
}
